In this notebook, a template is provided for you to implement your functionality in stages which is required to successfully complete this project. If additional code is required that cannot be included in the notebook, be sure that the Python code is successfully imported and included in your submission, if necessary. Sections that begin with 'Implementation' in the header indicate where you should begin your implementation for your project. Note that some sections of implementation are optional, and will be marked with 'Optional' in the header.
In addition to implementing code, there will be questions that you must answer which relate to the project and your implementation. Each section where you will answer a question is preceded by a 'Question' header. Carefully read each question and provide thorough answers in the following text boxes that begin with 'Answer:'. Your project submission will be evaluated based on your answers to each of the questions and the implementation you provide.
Note: Code and Markdown cells can be executed using the Shift + Enter keyboard shortcut. In addition, Markdown cells can be edited by typically double-clicking the cell to enter edit mode.
# Load pickled data
import pickle
# TODO: Fill this in based on where you saved the training and testing data
training_file = 'traffic-signs-data/train.p'
testing_file = 'traffic-signs-data/test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
X_test, y_test = test['features'], test['labels']
print ("X_train_Count : ",len(X_train))
print("y_train_count : ",len(y_train))
print("X_test_count : ",len(X_test))
print("y_test_count : ",len(y_test))
print("Shape of image before normalization:",X_train[0].shape)
from sklearn.model_selection import train_test_split
X_train_non_normalized, X_validation, y_train_non_normalized, y_validation = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
assert(len(X_train_non_normalized) == len(y_train_non_normalized))
assert(len(X_validation) == len(y_validation))
assert(len(X_test) == len(y_test))
print("Test Train data generated")
import cv2
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
matplotlib.rcParams['figure.figsize'] = (20.0, 10.0)
X_train = []
y_train = []
for i, (image, label) in enumerate(zip(X_train_non_normalized, y_train_non_normalized)):
zeros = np.zeros((32,32,3))
norm_image = cv2.normalize(image, zeros, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
X_train.append(norm_image)
y_train.append(label)
import random
mini_batch_size = 10
for start in range(0, 100, mini_batch_size):
fig, axes = plt.subplots(1,mini_batch_size)
end = start+mini_batch_size
for i, ((image, ax), label) in enumerate(zip(zip(X_train[start:end], axes), y_train[start:end])):
ax.set_title(label)
ax.imshow(image.squeeze())
plt.tight_layout()
plt.subplots_adjust(top=0.85)
plt.show()
The pickled data is a dictionary with 4 key/value pairs:
'features' is a 4D array containing raw pixel data of the traffic sign images, (num examples, width, height, channels).'labels' is a 2D array containing the label/class id of the traffic sign. The file signnames.csv contains id -> name mappings for each id.'sizes' is a list containing tuples, (width, height) representing the the original width and height the image.'coords' is a list containing tuples, (x1, y1, x2, y2) representing coordinates of a bounding box around the sign in the image. THESE COORDINATES ASSUME THE ORIGINAL IMAGE. THE PICKLED DATA CONTAINS RESIZED VERSIONS (32 by 32) OF THESE IMAGESComplete the basic data summary below.
### Replace each question mark with the appropriate value.
# TODO: Number of training examples
n_train = len(X_train)
# TODO: Number of testing examples.
n_test = len(X_test)
# TODO: What's the shape of an traffic sign image?
image_shape = X_train[0].shape
# TODO: How many unique classes/labels there are in the dataset.
n_classes = 43
print("Number of training examples =", n_train)
print("Number of testing examples =", n_test)
print("Image data shape =", image_shape)
print("Number of classes =", n_classes)
Visualize the German Traffic Signs Dataset using the pickled file(s). This is open ended, suggestions include: plotting traffic sign images, plotting the count of each sign, etc.
The Matplotlib examples and gallery pages are a great resource for doing visualizations in Python.
NOTE: It's recommended you start with something simple first. If you wish to do more, come back to it after you've completed the rest of the sections.
### Data exploration visualization goes here.
### Feel free to use as many code cells as needed.
# Visualizations will be shown in the notebook.
%matplotlib inline
index = random.randint(0, len(X_train))
image = X_train[index].squeeze()
plt.figure(figsize=(1,1))
plt.imshow(image)
print(y_train[index])
print("index :",index)
plt.subplot
"""
German Signs.
"""
matplotlib.rcParams['figure.figsize'] = (20.0, 10.0)
from matplotlib.pyplot import figure, show
from matplotlib.font_manager import FontProperties
# Example data
sign_id = np.arange(0,43)
sign_count = np.zeros(43)
Nr = 7
Nc = 6
fig = figure()
figtitle = 'German Signs'
t = fig.text(1.5, 4.5, figtitle,
verticalalignment ='top',
fontproperties=FontProperties(size=50))
w = 0.5
h = 0.5
image_list=[]
ax = []
for i in range(len(y_train)):
sign_count[y_train[i]] = sign_count[y_train[i]]+1
if (sign_count[y_train[i]]==1.0):
image = X_train[i].squeeze()
if (sign_count[y_train[i]]==1.0):
image = X_train[i].squeeze()
image_list.append(image)
k=0
for i in range(Nr):
for j in range(Nc):
pos = [0.075 + j*1.1*w, 0.18 + i*1.2*h, w, h]
a = fig.add_axes(pos)
a.imshow(image_list[k])
k +=1
ax.append(a)